package ar.com.larctest.providers.temp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import ar.com.larctest.dbHelper.SQLLARCTESTHelper;
import ar.com.larctest.entities.temp.TMeasurement;
import ar.com.larctest.entities.temp.TMeasurementItem;
import ar.com.larctest.utils.Constants;

public class MeasurementTempProvider {
    private static final String TAG = "MeasurementTempProvider";

    private static final String DB_MEASUREMENT_COLUMN_0_NAME = "idTrack";
    private static final String DB_MEASUREMENT_COLUMN_1_NAME = "idTemperature";
    private static final String DB_MEASUREMENT_COLUMN_2_NAME = "idHumidity";

    private static final String DB_MEASUREMENT_ITEM_COLUMN_0_NAME = "idHorse";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_1_NAME = "nameHorse";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_2_NAME = "initTime";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_3_NAME = "finishTime";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_4_NAME = "timeOut1";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_5_NAME = "timeOut2";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_6_NAME = "timeOut3";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_7_NAME = "lacticAcid1";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_8_NAME = "heartRate1";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_9_NAME = "lacticAcid2";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_10_NAME = "heartRate2";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_11_NAME = "lacticAcid3";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_12_NAME = "heartRate3";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_13_NAME = "respiratoryRate3";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_14_NAME = "idWork";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_15_NAME = "meters";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_16_NAME = "time";
    private static final String DB_MEASUREMENT_ITEM_COLUMN_17_NAME = "state";

    private SQLiteDatabase database;
    private String[] allColumnsMeasurement = {DB_MEASUREMENT_COLUMN_0_NAME, DB_MEASUREMENT_COLUMN_1_NAME, DB_MEASUREMENT_COLUMN_2_NAME};
    private String[] allColumnsMeasurementItems = {DB_MEASUREMENT_ITEM_COLUMN_0_NAME, DB_MEASUREMENT_ITEM_COLUMN_1_NAME, DB_MEASUREMENT_ITEM_COLUMN_2_NAME, DB_MEASUREMENT_ITEM_COLUMN_3_NAME, DB_MEASUREMENT_ITEM_COLUMN_4_NAME, DB_MEASUREMENT_ITEM_COLUMN_5_NAME, DB_MEASUREMENT_ITEM_COLUMN_6_NAME, DB_MEASUREMENT_ITEM_COLUMN_7_NAME, DB_MEASUREMENT_ITEM_COLUMN_8_NAME, DB_MEASUREMENT_ITEM_COLUMN_9_NAME, DB_MEASUREMENT_ITEM_COLUMN_10_NAME, DB_MEASUREMENT_ITEM_COLUMN_11_NAME, DB_MEASUREMENT_ITEM_COLUMN_12_NAME, DB_MEASUREMENT_ITEM_COLUMN_13_NAME, DB_MEASUREMENT_ITEM_COLUMN_14_NAME, DB_MEASUREMENT_ITEM_COLUMN_15_NAME, DB_MEASUREMENT_ITEM_COLUMN_16_NAME, DB_MEASUREMENT_ITEM_COLUMN_17_NAME};

    public MeasurementTempProvider(Context context) {
        SQLLARCTESTHelper dbHelper = SQLLARCTESTHelper.getHelper(context);
        database = dbHelper.getWritableDatabase();
    }

    public void insertOrUpdateMeasurement(TMeasurement measurement)
    {
        ContentValues contentValues;
        long nRowsEffected;

        contentValues = new ContentValues();

        contentValues.put(DB_MEASUREMENT_COLUMN_0_NAME, measurement.getIdTrack());
        contentValues.put(DB_MEASUREMENT_COLUMN_1_NAME, measurement.getIdTemperature());
        contentValues.put(DB_MEASUREMENT_COLUMN_2_NAME, measurement.getIdHumidity());

        nRowsEffected = database.update(SQLLARCTESTHelper.DB_MEASUREMENT_TEMP_TABLE_NAME, contentValues, null, null);
        if(nRowsEffected == 0){
            database.insert(SQLLARCTESTHelper.DB_MEASUREMENT_TEMP_TABLE_NAME, null, contentValues);
        }
    }

    public void insertMeasurementItem(TMeasurementItem measurementItem)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_DATETIME);

        ContentValues contentValues;

        contentValues = new ContentValues();

        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_0_NAME, measurementItem.getIdHorse());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_1_NAME, measurementItem.getNameHorse());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_2_NAME, measurementItem.getInitTime() == null ? null : sdf.format(measurementItem.getInitTime()));
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_3_NAME, measurementItem.getFinishTime() == null ? null : sdf.format(measurementItem.getFinishTime()));
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_4_NAME, measurementItem.getTimeOut1());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_5_NAME, measurementItem.getTimeOut2());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_6_NAME, measurementItem.getTimeOut3());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_7_NAME, measurementItem.getLacticAcid1());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_8_NAME, measurementItem.getHeartRate1());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_9_NAME, measurementItem.getLacticAcid2());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_10_NAME, measurementItem.getHeartRate2());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_11_NAME, measurementItem.getLacticAcid3());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_12_NAME, measurementItem.getHeartRate3());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_13_NAME, measurementItem.getRespiratoryRate3());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_14_NAME, measurementItem.getIdWork());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_15_NAME, measurementItem.getMeters());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_16_NAME, measurementItem.getTime());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_17_NAME, measurementItem.getState());

        database.insert(SQLLARCTESTHelper.DB_MEASUREMENT_TEMP_ITEM_TABLE_NAME, null, contentValues);
    }

    public void updateMeasurementItem(TMeasurementItem measurementItem)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_DATETIME);

        ContentValues contentValues;

        contentValues = new ContentValues();

        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_0_NAME, measurementItem.getIdHorse());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_1_NAME, measurementItem.getNameHorse());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_2_NAME, measurementItem.getInitTime() == null ? null : sdf.format(measurementItem.getInitTime()));
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_3_NAME, measurementItem.getFinishTime() == null ? null : sdf.format(measurementItem.getFinishTime()));
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_4_NAME, measurementItem.getTimeOut1());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_5_NAME, measurementItem.getTimeOut2());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_6_NAME, measurementItem.getTimeOut3());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_7_NAME, measurementItem.getLacticAcid1());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_8_NAME, measurementItem.getHeartRate1());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_9_NAME, measurementItem.getLacticAcid2());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_10_NAME, measurementItem.getHeartRate2());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_11_NAME, measurementItem.getLacticAcid3());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_12_NAME, measurementItem.getHeartRate3());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_13_NAME, measurementItem.getRespiratoryRate3());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_14_NAME, measurementItem.getIdWork());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_15_NAME, measurementItem.getMeters());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_16_NAME, measurementItem.getTime());
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_17_NAME, measurementItem.getState());

        database.update(SQLLARCTESTHelper.DB_MEASUREMENT_TEMP_ITEM_TABLE_NAME, contentValues, DB_MEASUREMENT_ITEM_COLUMN_0_NAME + " = " + measurementItem.getIdHorse(), null);
    }

    public void updateFinalMeasurementItem(Integer idHorse, Integer idWork, Double meters, Double time)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_DATETIME);

        ContentValues contentValues;

        contentValues = new ContentValues();

        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_14_NAME, idWork);
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_15_NAME, meters);
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_16_NAME, time);
        contentValues.put(DB_MEASUREMENT_ITEM_COLUMN_17_NAME, Constants.STATE_MEASUREMENT_FINAL_DATA);

        database.update(SQLLARCTESTHelper.DB_MEASUREMENT_TEMP_ITEM_TABLE_NAME, contentValues, DB_MEASUREMENT_ITEM_COLUMN_0_NAME + " = " + idHorse, null);
    }

    public boolean removeMeasurement()
    {
        //Delete Survey
        int result = database.delete(SQLLARCTESTHelper.DB_MEASUREMENT_TEMP_TABLE_NAME, null, null);
        return result > 0;
    }

    public boolean removeMeasurementItems()
    {
        //Delete Items
        int result = database.delete(SQLLARCTESTHelper.DB_MEASUREMENT_TEMP_ITEM_TABLE_NAME, null, null);
        return result > 0;
    }

    public boolean removeMeasurementItem(Integer idHorse)
    {
        //Delete Items
        int result = database.delete(SQLLARCTESTHelper.DB_MEASUREMENT_TEMP_ITEM_TABLE_NAME, DB_MEASUREMENT_ITEM_COLUMN_0_NAME + " = " + idHorse, null);
        return result > 0;
    }

    public TMeasurement getMeasurement()
    {
        Cursor cursor = null;
        TMeasurement measurement = null;

        try
        {
            cursor = database.query(SQLLARCTESTHelper.DB_MEASUREMENT_TEMP_TABLE_NAME, allColumnsMeasurement, null, null, null, null, null);
            cursor.moveToFirst();

            measurement = new TMeasurement();
            measurement.setIdTrack(cursor.getInt(0));
            measurement.setIdTemperature(cursor.getInt(1));
            measurement.setIdHumidity(cursor.getInt(2));

            return measurement;

        }
        catch (Exception e) {
            Log.e(TAG, e.getMessage());
            return measurement;
        }
        finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    public List<TMeasurementItem> getMeasurementItems()
    {
        Cursor cursor = null;
        List<TMeasurementItem> lstMeasurementItem = new ArrayList<TMeasurementItem>();

        try
        {
            SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_DATETIME);

            cursor = database.query(SQLLARCTESTHelper.DB_MEASUREMENT_TEMP_ITEM_TABLE_NAME, allColumnsMeasurementItems, null, null, null, null, null);
            cursor.moveToFirst();

            while (!cursor.isAfterLast()) {
                TMeasurementItem measurementItem = new TMeasurementItem();
                measurementItem.setIdHorse(cursor.getInt(0));
                measurementItem.setNameHorse(cursor.getString(1));
                measurementItem.setInitTime(cursor.getString(2) == null ? null : sdf.parse(cursor.getString(2)));
                measurementItem.setFinishTime(cursor.getString(3) == null ? null : sdf.parse(cursor.getString(3)));
                measurementItem.setTimeOut1(cursor.getString(4));
                measurementItem.setTimeOut2(cursor.getString(5));
                measurementItem.setTimeOut3(cursor.getString(6));
                measurementItem.setLacticAcid1(cursor.getDouble(7));
                measurementItem.setHeartRate1(cursor.getDouble(8));
                measurementItem.setLacticAcid2(cursor.getDouble(9));
                measurementItem.setHeartRate2(cursor.getDouble(10));
                measurementItem.setLacticAcid3(cursor.getDouble(11));
                measurementItem.setHeartRate3(cursor.getDouble(12));
                measurementItem.setRespiratoryRate3(cursor.getDouble(13));
                measurementItem.setIdWork(cursor.getInt(14));
                measurementItem.setMeters(cursor.getDouble(15));
                measurementItem.setTime(cursor.getDouble(16));
                measurementItem.setState(cursor.getString(17));
                measurementItem.setSelected(false);

                lstMeasurementItem.add(measurementItem);

                cursor.moveToNext();
            }

            return lstMeasurementItem;

        }
        catch (Exception e) {
            Log.e(TAG, e.getMessage());
            return lstMeasurementItem;
        }
        finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    public TMeasurementItem getMeasurementItem(Integer idHorse)
    {
        Cursor cursor = null;
        TMeasurementItem measurementItem = null;

        try
        {
            SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_DATETIME);

            cursor = database.query(SQLLARCTESTHelper.DB_MEASUREMENT_TEMP_ITEM_TABLE_NAME, allColumnsMeasurementItems, DB_MEASUREMENT_ITEM_COLUMN_0_NAME + " = " + idHorse, null, null, null, null);
            cursor.moveToFirst();

            measurementItem = new TMeasurementItem();
            measurementItem.setIdHorse(cursor.getInt(0));
            measurementItem.setNameHorse(cursor.getString(1));
            measurementItem.setInitTime(cursor.getString(2) == null ? null : sdf.parse(cursor.getString(2)));
            measurementItem.setFinishTime(cursor.getString(3) == null ? null : sdf.parse(cursor.getString(3)));
            measurementItem.setTimeOut1(cursor.getString(4));
            measurementItem.setTimeOut2(cursor.getString(5));
            measurementItem.setTimeOut3(cursor.getString(6));
            measurementItem.setLacticAcid1(cursor.getDouble(7));
            measurementItem.setHeartRate1(cursor.getDouble(8));
            measurementItem.setLacticAcid2(cursor.getDouble(9));
            measurementItem.setHeartRate2(cursor.getDouble(10));
            measurementItem.setLacticAcid3(cursor.getDouble(11));
            measurementItem.setHeartRate3(cursor.getDouble(12));
            measurementItem.setRespiratoryRate3(cursor.getDouble(13));
            measurementItem.setIdWork(cursor.getInt(14));
            measurementItem.setMeters(cursor.getDouble(15));
            measurementItem.setTime(cursor.getDouble(16));
            measurementItem.setState(cursor.getString(17));
            measurementItem.setSelected(false);

            return measurementItem;
        }
        catch (Exception e) {
            Log.e(TAG, e.getMessage());
            return measurementItem;
        }
        finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
}
